
* STEP 1 . Prepare replication data from Gollin and Udry (2021)
*---------------------------------------------------------
 

 
 *A. Obtain GU's microdata for Uganda
 *--------------
 

/*  We need to generate the file "$data/prebootstrap_UG.dta"
This dataset contains the microdata using in GU analysis. 
GU's replciation package doe snot provide this file but does provide the code and raw data to construct it. 
We use GU's code and data to replicate "$data/prebootstrap_UG.dta" and include it in our replicaiton package. 

If you want to replicate it yourself then;

1. Download the replication files of Gollin and Udry (2021) “Heterogeneity, Measurement Error,
 and Misallocation: Evidence from African Agriculture”, 
 Journal of Political Economy. Original package available at https://www.journals.uchicago.edu/doi/suppl/10.1086/711369
 We already provide a copy of these files needed for this replication but you can replace them if you want.

2. Save replication files in folder "Heterogeneity and Misallocation" in our replication package
 
 3. Run the code below (these dofiles are from GU's replication package  
 

   */
 
 
 do "$do/Uganda13_ECBv3.do"
do "$do/UG_data_prep.do"
do "$do/UG_prod_fnct_dataprep.do"



 

 *B. Obtain TFPA and TFPB (using two methods IVRC and 2SLS)
 *-------
 
 
 /*
 
 This code obtains plot-level TFPA and TFPB using the microdata from $data/prebootstrap_UG.dta"
 and IVRC and 2SLS estimates provided by GU. 
 These estimtes are stored in  "UG_boot_results_ivcrc.dta" and "UG_boot_results_2sls" in GU's replication package.
 
 The code I use is the same as GU's/ I extract it from 
 
   $do/UG_prod_fnct.do"
 
 I store the plot-level data with TFPA and TFPB in file
       "$FA_datasets/data_TFPs.dta"
*/

 
*IVRC
*------

{

use "${results}/UG_boot_results_ivcrc", clear                                    
  mkmat bCRC* if boot==0, mat(bCRC)
 local parameters "sigY sigL sigX sigeY sigeL sigeX sigLX sigYL sigYX"

mkmat ESTcovcrc* if boot==0, mat(ESTcovcrc)
matrix colnames ESTcovcrc = `parameters'
global sigeY = ESTcovcrc[1, colnumb(ESTcovcrc,"sigeY")]   /*late risk + meas error in output*/
global sigeL = ESTcovcrc[1, colnumb(ESTcovcrc,"sigeL")]  /*meas error in land*/
global sigeX= ESTcovcrc[1, colnumb(ESTcovcrc,"sigeX")]  /*meas error in labor*/




use "${data}/prebootstrap_UG", clear

qui d PShk*, varlist
global PShk = "`r(varlist)'"


qui d _LQ*, varlist
global lqvarnames = "`r(varlist)'"

*dropped CYRS*
qui d ag_advice , varlist
global qvarnames = "`r(varlist)'"


global xqvarnames = "xq1"
global factors l x
global RHS $PShk $lqvarnames  $xqvarnames $qvarnames
	

use "${data}/prebootstrap_UG", clear
	
	
	foreach var of varlist  y l x $RHS  {
		egen temp = mean(`var'), by(yearseasoncrpreg)
		replace `var' = `var' - temp
		drop temp
	}


qui d $RHS, varlist
global RHSnames = "`r(varlist)'"



matrix colnames bCRC = l x $RHSnames const 

scalar nv = colsof(bCRC) 

global alphal= bCRC[1,1]
global alphax= bCRC[1,2]
global const = bCRC[1,nv]

global sig_alpha_eps = $alphal ^2 * $sigeL + $alphax ^2 * $sigeX
*
* TFPA
*

gen double lnTFPA = $const   /*naive TFP*/



foreach var of varlist l x $RHSnames {
		replace lnTFPA = lnTFPA + bCRC[1, colnumb(bCRC,"`var'")] * `var' 
	}
	

replace lnTFPA = y - lnTFPA  
	sum lnTFPA 
		

global mulnTFPA = `r(mean)'
global sig2lnTFPA = `r(sd)'^2

*
*TFP B
*


*shrinking levels of TFP


global shrinkY = sqrt((($sig2lnTFPA -$sigeY - $sig_alpha_eps ))/(($sig2lnTFPA ))) 
gen lnTFPB = (lnTFPA - $mulnTFPA)*$shrinkY + $mulnTFPA

ren lnTFPB lnTFPB_ivrc
ren lnTFPA lnTFPA_ivrc

*TFP_B_lambda is same as TFP (eq 18 but adding a lambda (scaler) to the -sigma expression)
 *lambda=0: case of no measurement error , = TFPA
 *lambda=1: GU estimates of TFPB
 
  local i=0
  
 
 
 
save "$FA_datasets/data_ivrc.dta", replace

}
*
*

*2SLS

{
use "${results}/UG_boot_results_2sls", clear                                      

mkmat b2sls* if boot==0, mat(b2sls)
local parameters "sigY sigL sigX sigeY sigeL sigeX sigLX sigYL sigYX"

mkmat ESTcov2sls* if boot==0, mat(ESTcov2sls)
matrix colnames ESTcov2sls = `parameters'
global sigeY = ESTcov2sls[1, colnumb(ESTcov2sls,"sigeY")]   /*late risk + meas error in output*/
global sigeL = ESTcov2sls[1, colnumb(ESTcov2sls,"sigeL")]  /*mean error in land*/
global sigeX= ESTcov2sls[1, colnumb(ESTcov2sls,"sigeX")]  /*mean error in labor*/

use "${data}/prebootstrap_UG", clear

qui d PShk*, varlist
global PShk = "`r(varlist)'"


qui d _LQ*, varlist
global lqvarnames = "`r(varlist)'"

*dropped CYRS*
qui d ag_advice , varlist
global qvarnames = "`r(varlist)'"


global xqvarnames = "xq1"
global factors l x
global RHS $PShk $lqvarnames  $xqvarnames $qvarnames
	
foreach var of varlist  y l x $RHS {
		egen temp = mean(`var'), by(yearseasoncrpreg)
		replace `var' = `var' - temp
		drop temp
	}


qui d $RHS, varlist
global RHSnames = "`r(varlist)'"



matrix colnames b2sls = l x $RHSnames const 

scalar nv = colsof(b2sls) 

global alphal= b2sls[1,1]
global alphax= b2sls[1,2]
global const = b2sls[1,nv]

global sig_alpha_eps = $alphal ^2 * $sigeL + $alphax ^2 * $sigeX
*
* TFPA
*

gen double lnTFPA = $const   /*naive TFP*/



foreach var of varlist l x $RHSnames {
		replace lnTFPA = lnTFPA + b2sls[1, colnumb(b2sls,"`var'")] * `var' 
	}
	

replace lnTFPA = y - lnTFPA  
	sum lnTFPA 
		

global mulnTFPA = `r(mean)'
global sig2lnTFPA = `r(sd)'^2

*
*TFP B
*


*shrinking levels of TFP


global shrinkY = sqrt((($sig2lnTFPA -$sigeY - $sig_alpha_eps ))/(($sig2lnTFPA ))) 
gen lnTFPB = (lnTFPA - $mulnTFPA)*$shrinkY + $mulnTFPA



ren lnTFPB lnTFPB_2sls
ren lnTFPA lnTFPA_2sls


*TFP_B_lambda is same as TFP (eq 18 but adding a lambda (scaler) to the -sigma expression)
 *lambda=0: case of no measurement error , = TFPA
 *lambda=1: GU estimates of TFPB
 
  local i=0
  
 

save "$FA_datasets/data_2sls.dta", replace
}


*
 
 
* Merge TFPs
*--------------

use "$FA_datasets/data_ivrc.dta", clear
merge 1:1 HHID year season parcelID plotID  using "$FA_datasets/data_2sls.dta", keepusing(lnTFPB_2sls* lnTFPA_2sls*) nogen

*I can add other dataset here as well.. 
*need to  improve HH level regressions
save "$FA_datasets/data_TFP_UGA.dta", replace

}

